/**
 * 
 */
package org.wicket.jbpm.dao.user;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.wicket.jbpm.model.User;

/**
 * @author Emmanuel Nollase - emanux
 * created: May 10, 2009 - 3:15:06 PM
 */
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@SuppressWarnings("unchecked")
	public List<User> findByGrpId(String grpId) throws DataAccessException {
		return this.getSession().createCriteria(User.class).add(Restrictions.eq("group.id", grpId)).list();
	}

	public boolean checkUser(String username, String password)
			throws DataAccessException {
		final Criteria crit = this.getSession().createCriteria(User.class);
		crit.add(Restrictions.eq("cd", username));
		crit.add(Restrictions.eq("password", password));
		
		return crit.uniqueResult() != null;
	}

	public User loadUser(String username, String password)
			throws DataAccessException {
		final Criteria crit = this.getSession().createCriteria(User.class);
		crit.add(Restrictions.eq("cd", username));
		crit.add(Restrictions.eq("password", password));
		
		return (User) crit.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<String> findForJbpmUser(String grpId)
		throws DataAccessException
	{
	    final Criteria crit = this.getSession().createCriteria(User.class);
	    crit.setProjection(Projections.property("cd"));
	    crit.add(Restrictions.eq("group.id", grpId));
	    return crit.list();
	}
}
